package top.gangster.lunan.userservice.controller;


import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import top.gangster.lunan.result.Result;
import top.gangster.lunan.userservice.annotation.LoginToken;
import top.gangster.lunan.userservice.annotation.PermissionLimit;
import top.gangster.lunan.userservice.entity.Department;
import top.gangster.lunan.userservice.entity.vo.DeptVo;
import top.gangster.lunan.userservice.mapper.DepartmentMapper;
import top.gangster.lunan.userservice.service.DepartmentService;

/**
 * <p>
 * 公司所含部门信息 前端控制器
 * </p>
 *
 * @author vic
 * @since 2021-11-06
 */
@RestController
@RequestMapping("/userservice/department")
public class DepartmentController {

  @Autowired
  DepartmentService departmentService;

  /**
   * 列表查看dept
   * @param current 当前页
   * @param size 当前页面大小
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/dept")
  public Result getDeptListController(int current,int size){
    DeptVo deptList = departmentService.getDeptList(current,size);
    if (deptList == null)
    {
      return Result.error().message("查询失败，未找到任何部门！");
    }
    return Result.ok().data("deptList",deptList);
  }

  /**
   * 通过部门名字查看dept
   * @param current 当前页
   * @param size 当前页面大小
   * @param deptName 部门名字
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/deptByname")
  public Result getDeptByNameController(int current,int size,String deptName){
    DeptVo deptList = departmentService.getDeptByName(current,size,deptName);
    if (deptList == null)
    {
      return Result.error().message("查询失败，未找到任何部门！");
    }
    return Result.ok().data("deptList",deptList);
  }

  /**
   * 增加部门
   * @param dept 部门
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @PostMapping("/addDept")
  public Result addDeptController(@RequestBody Department dept){
    boolean b = departmentService.addDept(dept);
    if (!b){
      return Result.error().message("新增部门失败！请检查信息输入是否正确！");
    }
    return Result.ok().message("新增部门成功！");
  }

  /**
   * 修改部门
   * @param dept 部门
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @PutMapping("/modifyDept")
  public Result modifyDeptController(@RequestBody Department dept){
    boolean b = departmentService.modifyDept(dept);
    if (!b){
      return Result.error().message("修改部门失败！请检查信息输入是否正确！");
    }
    return Result.ok().message("修改部门成功！");
  }

  /**
   * 删除部门
   * @param dept 部门
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @DeleteMapping ("/deleteDept")
  public Result deleteDeptController(@RequestBody Department dept){
    boolean b = departmentService.deleteDept(dept);
    if (!b){
      return Result.error().message("删除部门失败！请检查信息输入是否正确！");
    }
    return Result.ok().message("删除部门成功！");
  }

  /**
   * 不分页查看dept列表
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/deptList")
  public Result getDeptListController(){
    List<Department> deptList = departmentService.getDeptList();
    if (deptList == null){
      return Result.error().message("未查找到任何部门！");
    }
    return Result.ok().data("deptList",deptList);
  }

}

